#include "FileSystem.h"

void FileSystem::Init()
{
	initialized = true;
}

void FileSystem::Shutdown()
{
	initialized = false;
}
	
bool FileSystem::IsInitialized()
{
	return initialized = true;
}

bool FileSystem::CreateDir(const char* path)
{
	bool ret = CreateDirectory(path, NULL);

	if(gDebug && ret == false)
	{
		Con::Printf("Cannot create direcotry: %s \n", path);

		//error check
		DWORD err = GetLastError();
		if(err == ERROR_ALREADY_EXISTS)
			Con::Printf("Directory already exists\n");
		if(err == ERROR_PATH_NOT_FOUND)
			Con::Printf("Directory path not found\n");
	}

	return ret;
}
	
bool FileSystem::DeleteDir(const char* path)
{
	bool ret = RemoveDirectory(path);

	if(gDebug && ret == false)
	{
		Con::Printf("Cannot remove directory: %s \n", path);
	}

	return ret;
}

bool FileSystem::MoveFile(const char* src, const char* dst)
{
	bool ret = ::MoveFileExA(src, dst, MOVEFILE_COPY_ALLOWED);

	if(gDebug && ret == false)
	{
		Con::Printf("Fail to move file: %s", src);
	}

	return ret;
}
	
bool FileSystem::CopyFile(const char* pathSrc, const char* pathDst)
{
	bool ret = ::CopyFile(pathSrc, pathDst, TRUE);

	if(gDebug && ret == false)
	{	
		Con::Printf("Fail to copy file: %s", pathSrc);
	}

	return ret;
}
	
bool FileSystem::DelFile(const char* filename)
{
	bool ret = DeleteFile(filename);

	if(gDebug && ret == false)
	{
		Con::Printf("Cannot delete file: %s \n", filename);

		//error check
		DWORD err = GetLastError();
		if(err == ERROR_FILE_NOT_FOUND)
			Con::Printf("File not found\n");
		if(err == ERROR_ACCESS_DENIED)
			Con::Printf("Access denied\n");
	}
	
	return ret;
}

FileInfo FileSystem::GetFileInfo(File file) const
{
	HANDLE hFile = file.GetHandle();
	DWORD fileSize;
	FILETIME creationTime, writeTime, accessTime;
	SYSTEMTIME sysCreationTime, sysWriteTime, sysAccessTime;
	FileInfo fileInfo;

	GetFileTime(hFile, &creationTime, &accessTime, &writeTime);
	FileTimeToSystemTime(&creationTime, &sysCreationTime);
	FileTimeToSystemTime(&accessTime, &sysAccessTime);
	FileTimeToSystemTime(&writeTime, &sysWriteTime);
	GetFileSize(hFile, &fileSize);

	fileInfo.creationTime.year       = sysCreationTime.wYear;
	fileInfo.creationTime.month      = sysCreationTime.wMonth;
	fileInfo.creationTime.day        = sysCreationTime.wDay;
	fileInfo.creationTime.hour       = sysCreationTime.wHour;
	fileInfo.creationTime.minute     = sysCreationTime.wMinute;
	fileInfo.creationTime.second     = sysCreationTime.wSecond;
	fileInfo.creationTime.milisecond = sysCreationTime.wMilliseconds;

	fileInfo.lastAccessTime.year       = sysAccessTime.wYear;
	fileInfo.lastAccessTime.month      = sysAccessTime.wMonth;
	fileInfo.lastAccessTime.day        = sysAccessTime.wDay;
	fileInfo.lastAccessTime.hour       = sysAccessTime.wHour;
	fileInfo.lastAccessTime.minute     = sysAccessTime.wMinute;
	fileInfo.lastAccessTime.second     = sysAccessTime.wSecond;
	fileInfo.lastAccessTime.milisecond = sysAccessTime.wMilliseconds;

	fileInfo.lastWriteTime.year       = sysWriteTime.wYear;
	fileInfo.lastWriteTime.month      = sysWriteTime.wMonth;
	fileInfo.lastWriteTime.day        = sysWriteTime.wDay;
	fileInfo.lastWriteTime.hour       = sysWriteTime.wHour;
	fileInfo.lastWriteTime.minute     = sysWriteTime.wMinute;
	fileInfo.lastWriteTime.second     = sysWriteTime.wSecond;
	fileInfo.lastWriteTime.milisecond = sysWriteTime.wMilliseconds;

	fileInfo.fileSize = static_cast<u32>(fileSize);

	return fileInfo;

}